package gui;

import database.entity.Contact;
import database.entity.ContactGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Tom
 */
public class UIContact extends javax.swing.JDialog 
{
    public static enum ContactViewMode
    {
        VIEW,
        NEW
    }
    
    UIMain main;
    Contact contact;
    ContactViewMode mode;
    
    public UIContact(UIMain main, Contact ct, ContactViewMode mode) 
    {
        super(main, true);
        this.main = main;
        this.contact=ct;
        this.mode=mode;
        initComponents();
        initInterface();          
        setLocationRelativeTo(null);
    }
    
    private void initInterface()
    {
        //load groups into cboxGroup
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (ContactGroup cg : main.user.contactgroup) 
        {
            model.addElement(cg.groupName);
        }
        cboxGroup.setModel(model);
        
        switch (mode)
        {
            case VIEW:        
                btOK.setVisible(true);
                btEdit.setVisible(true);
                btSave.setVisible(false);
                btCancel.setVisible(false);
                
                txtName.setEditable(false);
                txtSignature.setEditable(false);
                txtEmail.setEditable(false);
                
                cboxGroup.setEnabled(false);
                
                txtName.setText(contact.ContactName);
                txtEmail.setText(contact.Email);
                txtSignature.setText(contact.PublicKey);
                break;        
            case NEW:
                btOK.setVisible(false);
                btEdit.setVisible(false);
                btSave.setVisible(true);
                btCancel.setVisible(true);
                txtName.requestFocus();                
                break;
        }
    }
    
    private void enableEdit(boolean enabled)
    {
        btEdit.setVisible(!enabled);
        btOK.setVisible(!enabled);
        btSave.setVisible(enabled);
        btCancel.setVisible(enabled);
        
        txtName.setEditable(enabled);
        txtSignature.setEditable(enabled);
        txtEmail.setEditable(enabled);
        cboxGroup.setEnabled(enabled);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cboxGroup = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtSignature = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        btOK = new javax.swing.JButton();
        btEdit = new javax.swing.JButton();
        btCancel = new javax.swing.JButton();
        btSave = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Nom du contact");

        jLabel3.setText("Group");

        txtSignature.setColumns(20);
        txtSignature.setRows(5);
        jScrollPane1.setViewportView(txtSignature);

        jLabel4.setText("Signature");

        btOK.setText("OK");
        btOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btOKActionPerformed(evt);
            }
        });

        btEdit.setText("Modifier");
        btEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditActionPerformed(evt);
            }
        });

        btCancel.setText("Annuler");
        btCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelActionPerformed(evt);
            }
        });

        btSave.setText("Sauvegarder");
        btSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btSave, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btOK, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btOK, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btSave, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jLabel2.setText("Email");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(100, 100, 100)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cboxGroup, 0, 419, Short.MAX_VALUE)
                            .addComponent(txtName)
                            .addComponent(txtEmail)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(cboxGroup, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(46, 46, 46))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {txtEmail, txtName});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
        switch (mode)
        {
            case NEW:
                this.dispose();
                break;
            case VIEW:
                enableEdit(false);
                break;
        }        
    }//GEN-LAST:event_btCancelActionPerformed

    private void btEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditActionPerformed
        enableEdit(true);
    }//GEN-LAST:event_btEditActionPerformed

    private void btOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOKActionPerformed
        this.dispose();
    }//GEN-LAST:event_btOKActionPerformed

    private void btSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSaveActionPerformed
                       
        String name = txtName.getText().trim();
        String email = txtEmail.getText().trim();
        String publicKey = txtSignature.getText();
        String groupName = (String)cboxGroup.getSelectedItem();
        if ("".equals(name))
        {
            JOptionPane.showMessageDialog(this, "Donnez ce contact un nom!", "", JOptionPane.INFORMATION_MESSAGE);
        }
        else if ("".equals(email))
        {
            JOptionPane.showMessageDialog(this, "Saisissez l'email du contact!", "", JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {                            
            switch (mode)
            {
                case NEW:                        
                    //check if email address existed
                    Contact ct = main.user.databaseWorker.getContactFromEmail(email);               
                    if (ct!=null)
                    {
                        JOptionPane.showMessageDialog(this, "Ce contact a déjà existé comme "+ct.ContactName);
                    }
                    else
                    {
                        //add new contact
                        main.user.databaseWorker.addContact(name, email, groupName, publicKey);                        
                        JOptionPane.showMessageDialog(this, "Contact ajouté", "", JOptionPane.INFORMATION_MESSAGE);
                        //refresh list
                        main.loadContactMode();
                        dispose();
                    }
                    break;
                case VIEW:
                    //update current contact
                    main.user.databaseWorker.updateContactProperties(contact, name, email, groupName, publicKey);                        
                    JOptionPane.showMessageDialog(this, "Changements enregistrés", "", JOptionPane.INFORMATION_MESSAGE);
                    enableEdit(false);
                    //refresh UI
                    main.loadContactMode();
                    break;
            }            
        }                        
    }//GEN-LAST:event_btSaveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancel;
    private javax.swing.JButton btEdit;
    private javax.swing.JButton btOK;
    private javax.swing.JButton btSave;
    private javax.swing.JComboBox cboxGroup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextArea txtSignature;
    // End of variables declaration//GEN-END:variables
}
